<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">

          <a-col :md="6" :sm="8">
            <a-form-item label="订单号">
              <a-input placeholder="请输入订单号" v-model="queryParam.orderNumber"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="合作编号">
              <a-input placeholder="请输入合作编号" v-model="queryParam.takeOrders"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="订单类型">
              <a-select placeholder="请输入订单类型" v-model="queryParam.orderType">
                <a-select-option v-for="item in orderTypes" :key="item.key" :value="item.key">{{ item.val }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="订单状态">
              <a-select placeholder="请选择订单状态" v-model="queryParam.orderStatus">
                <a-select-option v-for="item in orderStatusSetect" :key="item.key" :value="item.key">{{ item.val }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col v-if="showMarkd" :md="6" :sm="8">
            <a-form-item label="子管理结算状态">
              <a-select placeholder="请选择结算状态" v-model="queryParam.managerClose">
                <a-select-option v-for="item in managerCloses" :key="item.key" :value="item.key">{{ item.val }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
<!--          <a-col :md="6" :sm="8" >
          </a-col>-->
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">发布订单</a-button>
      <a-button v-if="showExportXls" type="primary"  icon="download" @click="handleExportXls">导出</a-button>
      <span style="float: right;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
      </span>
<!--            <a-upload name="file" :showUploadList="false" :multiple="false" :action="importExcelUrl" @change="handleImportExcel">
              <a-button type="primary" icon="import">导入</a-button>
            </a-upload>-->
      <a-dropdown v-if="selectedRowKeys.length > 0 && showMarkd">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchMark"><a-icon type="check-circle"/>标记为子管理已结算</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a v-if="showExportXls && record.orderStatus!=6 && record.orderStatus!=5" @click="handleclose(record)">确认结算</a>
           <a-divider v-if="showExportXls && record.orderStatus!=6 && record.orderStatus!=5" type="vertical" />
          <a v-if="showCloseImg(record)" @click="uplodclose(record)">上传结算图</a>
          <a-divider v-if="showCloseImg(record)" type="vertical" />
          <a v-if="record.orderStatus!=0 && record.orderStatus!=6 && record.orderStatus!=5" @click="handleEditStatus(record)">修改状态</a>
          <a-divider v-if="record.orderStatus!=0 && record.orderStatus!=6 && record.orderStatus!=5" type="vertical" />
          <a v-if="record.orderStatus==0" @click="handleEdit(record)">编辑</a>
          <a-divider v-if="record.orderStatus==0" type="vertical" />
           <a href="javascript:;" @click="handleDetail(record)">详情</a>
           <a-divider v-if="record.orderStatus==0" type="vertical" />
           <a-popconfirm v-if="record.orderStatus==0" title="数据将彻底清除,确定删除吗?" @confirm="() => handleDelete(record.id)">
           <a>删除</a>
           </a-popconfirm>
        </span>
        <span slot="cost" slot-scope="text">
          <div v-if="text">{{text}}元</div>
        </span>
        <span slot="closingCost" slot-scope="text, record">
         <div style="width: 180px" class="contText"  >加价:{{record.raiseNumber?record.raiseNumber+'元':'暂无'}}，结算{{Number(record.raiseNumber)+Number(record.cost)}}元</div>
        </span>
        <span slot="fieldText" slot-scope="text">
          <div style="width: 220px"  class="contText" :title="text">{{ text }}</div>
        </span>
        <span slot="orderStatus" slot-scope="text,record">
                <a-tag  :color="getColor(record)" style="width: 100%;color: rgba(0, 0, 0, 0.65);height: 38px;font-size: 14px;line-height: 2.5;" >{{text}}</a-tag>
        </span>
        <span slot="takeManagerMoney" slot-scope="text,record">
          <div v-if="text">{{Number(text)*(Number(record.commissionRate)/100)}}元</div>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <orderForm-modal ref="modalForm" @ok="modalFormOk"></orderForm-modal>
    <editOrderFormModal ref="EditStatusModalForm" @ok="modalFormOk"></editOrderFormModal>
    <UploadImgByOrder ref="uploadImgByOrder" @ok="modalFormOk" ></UploadImgByOrder>
    <CloseOrderFormModal ref="closeOrderFormModal" @ok="modalFormOk"></CloseOrderFormModal>
  </a-card>
</template>

<script>
import CloseOrderFormModal from './modules/CloseOrderFormModal'
import EditOrderFormModal from './modules/EditOrderFormModal'
import OrderFormModal from './modules/OrderFormModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import {Dict}   from '@/api/Dict'
import UploadImgByOrder from "@comp/tools/UploadImgByOrder";
import {mapGetters} from "vuex";
import {deleteAction, getAction} from "@api/manage";

export default {
  name: "OrderFormList",
  mixins:[JeecgListMixin,Dict],
  components: {
    OrderFormModal,
    EditOrderFormModal,
    UploadImgByOrder,
    CloseOrderFormModal
  },
  data () {
    return {
      description: '订单管理页面',
      showExportXls:false,
      showMarkd:false,
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key:'rowIndex',
          width:30,
          align:"center",
          customRender:function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        {
          title: '创建时间',
          align:"center",
          dataIndex: 'createTime',
          width: 160
        },
        /*{
          title: '编号',
          align:"center",
          dataIndex: 'id',
          width: 30
        },*/
        {
          title: '合作编号',
          align:"center",
          dataIndex: 'takeOrders',
        },
        {
          title: '订单号',
          align:"center",
          dataIndex: 'orderNumber',
        },
        {
          title: '订单类型',
          align:"center",
          dataIndex: 'orderType_dictText',
        },
        {
          title: '指派人',
          align:"center",
          dataIndex: 'createByName',
        },
        {
          title: '要求完成时间',
          align:"center",
          dataIndex: 'finishTime',
          width: 160
        },
        {
          title: '结算费用',
          align:"center",
          dataIndex: 'raiseNumber',
          scopedSlots: { customRender: 'closingCost' },
          width: 180,
          ellipsis: true,
        },
        {
          title: '子管理提成数',
          align:"center",
          dataIndex: 'takeManagerMoney',
          scopedSlots: { customRender: 'cost' },
          width: 80
        },
        {
          title: '接单金额',
          align:"center",
          dataIndex: 'takeMoney',
          scopedSlots: { customRender: 'cost' },
        },
        {
          title: '订单状态',
          align:"center",
          dataIndex: 'orderStatus_dictText',
          scopedSlots: { customRender: 'orderStatus' },
          width: 160
        },
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          scopedSlots: { customRender: 'action' },
        }
      ],
      url: {
        list: "/orderForm/orderForm/list",
        delete: "/orderForm/orderForm/delete",
        deleteBatch: "/orderForm/orderForm/deleteBatch",
        MarkBatch: "/orderForm/orderForm/MarkBatch",
        exportXlsUrl: "orderForm/orderForm/exportXls",
        importExcelUrl: "orderForm/orderForm/importExcel",
      },
    }
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    }
  },
  created() {
    if(this.userInfo().rolestr=='admin'){
      this.showExportXls=true;
    }
    if(this.userInfo().rolestr=='admin' || this.userInfo().rolestr=='accounts'){
      this.showMarkd=true;
      this.columns.splice(2,0,{
        title: '子管理结算状态',
        align:"center",
        dataIndex: 'managerClose_dictText',
        width: 60
      });
    }

  },
  methods: {
    ...mapGetters(["userInfo"]),
    batchMark: function () {
      if(!this.url.MarkBatch){
        this.$message.error("请设置url.MarkBatch属性!")
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return;
      } else {
        var ids = "";
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          if(this.selectionRows[a].orderStatus!=5){
            this.$message.warning('选择的数据中有未结算订单,不能为子管理标记!');
            return;
          }
          ids += this.selectedRowKeys[a] + ",";
        }
        var that = this;
        this.$confirm({
          title: "确认标记",
          content: "是否标记选中数据?",
          onOk: function () {
            deleteAction(that.url.MarkBatch, {ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.loadData();
                that.onClearSelected();
              } else {
                that.$message.warning(res.message);
              }
            });
          }
        });
      }
    },
    handleclose(record){
      console.log("orderStatus-->",record.orderStatus);
      this.$refs.closeOrderFormModal.edit(record);
      this.$refs.closeOrderFormModal.title="结算";

    },
    loadData(arg) {
      if(!this.url.list){
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.setselectData(this.getQueryParams());//查询条件
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
      })
    },
    /* 导出 */
    handleExportXls(){
      var params =this.setselectData(this.getQueryParams());
      let paramsStr = encodeURI(JSON.stringify(params));
      let url = `${window._CONFIG['domianURL']}/${this.url.exportXlsUrl}?paramsStr=${paramsStr}`;
      window.location.href = url;
    },
    setselectData(params){
      if(params.takeOrders){
        params.takeOrders="*"+params.takeOrders+"*";
      }
      if(this.userInfo().rolestr=='admin' || this.userInfo().rolestr=='accounts'){
      }else {
        params.createBy=this.userInfo().username;
      }
      return params;
    },
    showCloseImg(record){
     let rolestr= this.$store.getters.userInfo.rolestr;
      if(record.orderStatus==5 && !record.closeImg  && (rolestr=='admin' ||  rolestr=='accounts')){
       return true
      }
      return false
    },
    uplodclose(record){
      this.$refs.uploadImgByOrder.edit(record);
      this.$refs.uploadImgByOrder.visible=true;
    },
    handleEditStatus(record){
      this.$refs.EditStatusModalForm.edit(record);
      let orderStatusList=[
        {key:0,val:'进行中'},
        {key:1,val:'已发货，待客户确认'},
        {key:2,val:'已交货，申请发货'},
        {key:3,val:'可申请结算'},
        {key:4,val:'订单不满意退款'},
        ];
     /*let rolestr= this.$store.getters.userInfo.rolestr;
      if (rolestr=='admin'|| rolestr=='accounts'){
        orderStatusList.push({key:5,val:'已结算'});
      }*/
      this.$refs.EditStatusModalForm.setOrderStatusList(orderStatusList);
      this.$refs.EditStatusModalForm.title = "修改状态";
      this.$refs.EditStatusModalForm.disableSubmit = false;
    },
  }
}
</script>
<style lang="less" scoped>
/** Button按钮间距 */
.ant-btn {
  margin-left: 3px
}
.ant-card-body .table-operator{
  margin-bottom: 18px;
}
.ant-table-tbody .ant-table-row td{
  padding-top:15px;
  padding-bottom:15px;
}
.anty-row-operator button{margin: 0 5px}
.ant-btn-danger{background-color: #ffffff}

.ant-modal-cust-warp{height: 100%}
.ant-modal-cust-warp .ant-modal-body{height:calc(100% - 110px) !important;overflow-y: auto}
.ant-modal-cust-warp .ant-modal-content{height:90% !important;overflow-y: hidden}
.contText{
  overflow: hidden;/*内容超出后隐藏*/
  text-overflow: ellipsis;/* 超出内容显示为省略号*/
  white-space: nowrap;/*文本不进行换行*/
}
</style>